var layer = layui.layer
const form = layui.form
layui.use(function () {
    var laydate = layui.laydate;
    // 渲染
    laydate.render({
        elem: '#ID-laydate-demo'
    })
})


// todo ================== 富文本编辑器 =================================

const E = window.wangEditor
const editor = new E("#contentBox")
// 或者 const editor = new E(document.getElementById('div1'))
editor.config.zIndex = 500 // 修改层级，避免挡住下拉框
editor.create()

// todo ================== 阅读次数模块 =================================
$('.add').on('click', function () {
    let num = $('[name=click]').val()
    if (num >= 0) $('[name=click]').prop('disabled', false)
    $('[name=click]').val(++num)
})

$('.sub').on('click', () => {
    let num = $('[name=click]').val()
    if (num <= 1) return $('[name=click]').prop('disabled', true)
    $('[name=click]').val(--num)
})

// 渲染列表选择框
axios
    .get('/ydd_cate/all')
    .then(({ data: { data } }) => {
        $('#zx_select').append(data.map(({ id, catename }) => `
                <option value="${id}">${catename}</option>
           `
        ).join(''))
        form.render('select') //刷新select选择框渲染
    })

// 渲染文章标签
axios
    .get('/ydd_tag/all')
    .then(({ data: { data } }) => {
        $('.zx_tags').empty().append(data.map(({ id, tagname }) => `
        <input type="checkbox" name="tags" value=${tagname} data-id=${id} title=${tagname}>
        `).join(''))
        form.render()
    })


// todo ================== 文章缩略图 =================================

// ! 点击按钮，触发文件选择框的点击事件
$('#btnChooseCoverImage').on('click', function () {
    $('#fileCover_add').click()
})

$('#fileCover_add').on('change', function () {
    const files = this.files[0]
    const fd = new FormData()
    fd.append('file', files)
    axios
        .post('/common/upload?type=images', fd)
        .then(({ data: { data: { savePath } } }) => {
            $('#zx_addImgSrc').prop("src", `http://124.223.14.236:8060/${savePath}`)
        })
})

function checklist() {
    let list = document.querySelectorAll('.zx_tags input[type="checkbox"]')
    let listdata = []
    list.forEach(item => {
        if (item.checked) {
            listdata.push(item.value)
        }
    })
    const tagslist = listdata.join()
    return tagslist
}

// 发布文章
form.on('submit(formDemo)', function (data) {
    const tagsText = checklist()
    const qw = editor.txt.html()
    const imgSrc = $('#zx_addImgSrc').prop("src").split('8060/')[1].replace(/\//g, '')
    var field = data.field
    const time = +new Date(field.create_date)

    field.status = "1"
    // field.create_date = +new Date(field.create_date)
    field.ishot = field.ishot ? 1 : 0
    field.istop = field.istop ? 1 : 0
    field.pic = imgSrc
    field.content = qw
    field.tags = tagsText
    field.create_date = time
    field.cateid = +field.cateid
    field.click = +field.click

    axios.post('/ydd_article/add', field)
        .then(res => {
            $('#addForm')[0].reset()
            editor.txt.html('')
            layer.msg('发布成功', {
                icon: 1,
                time: 1500
            }, function () {
                window.parent.document.querySelector('#zx_art').click()
            })
        })
    return false
})

// 保存草稿
form.on('submit(saveDemo)', function (data) {
    const tagsText = checklist()
    const qw = editor.txt.html()
    const imgSrc = $('#zx_addImgSrc').prop("src").split('8060/')[1].replace(/\//g, '')
    var field = data.field
    const time = +new Date(field.create_date)

    field.status = "2"
    // field.create_date = +new Date(field.create_date)
    field.ishot = field.ishot ? 1 : 0
    field.istop = field.istop ? 1 : 0
    field.pic = imgSrc
    field.content = qw
    field.tags = tagsText
    field.create_date = time
    field.cateid = +field.cateid
    field.click = +field.click

    axios.post('/ydd_article/add', field)
        .then(res => {
            $('#addForm')[0].reset()
            editor.txt.html('')
            layer.msg('保存成功', {
                icon: 1,
                time: 1500
            }, function () {
                window.parent.document.querySelector('#zx_art').click()
            })
        })
    return false
})

$('#zx_back').on('click', function () {
    layer.confirm('是否确认要跳转？', { icon: 3, title: '提示' }, function (index) {
        window.parent.document.querySelector('#zx_art').click()
        layer.close(index);
    })
}) 